<?php

declare(strict_types=1);

namespace app\shop\controller\admin\data;

use sheep\controller\Backend;
use app\Request;
use app\shop\model\data\Area as AreaModel;
use sheep\controller\traits\Crud;
use think\facade\Db;
use think\Response;

class Area extends Backend
{
    use Crud;

    protected $model = null;
    protected $isPaginate = false;
    protected $onlyParams = ['id', 'pid', 'name'];

    public function initialize()
    {
        $this->model = new AreaModel();
    }

    public function list(Request $request): Response
    {

        $list = $this->model->sheepFilter()->with('children.children')->where('pid', 0)->select();               // 查询全部

        return success('获取成功', $list);
    }

    public function select(Request $request): Response
    {
        $level = $request->param('level', 'all');
        $with = ['children.children'];
        if ($level == 'city') {
            $with = ['children'];
        } else if ($level == 'province') {
            $with = [];
        }
        $list = $this->model->with($with)->where('pid', 0)->field('id, name, pid, level')->select();               // 查询全部

        return success('获取成功', $list);
    }

    /**
     * 添加
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        if (!empty($this->onlyParams)) {                        // 参数过滤
            $params = $request->only($this->onlyParams);
        } else {                                                // 接受全部参数
            $params = $request->param();
        }
        $params['level'] = 'province';
        if (isset($params['pid']) && $params['pid']) {
            $parent = $this->model->findOrFail($params['pid']);
            if ($parent['level'] == 'province') {
                $params['level'] = 'city';
            } else if ($parent['level'] == 'city') {
                $params['level'] = 'district';
            }
        }
        $result = Db::transaction(function () use ($params) {
            return $this->model->save($params);
        });
        if ($result) {
            return success('保存成功', $this->model);
        }
        return error('保存失败');
    }

    /**
     * 编辑(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function edit(Request $request, $id)
    {
        if (!empty($this->onlyParams)) {                        // 参数过滤
            $params = $request->only($this->onlyParams);
        } else {                                                // 接受全部参数
            $params = $request->param();
        }

        $pk = $this->model->getPk();
        $params['level'] = 'province';
        if (isset($params['pid']) && $params['pid']) {
            $parent = $this->model->findOrFail($params['pid']);
            if ($parent['level'] == 'province') {
                $params['level'] = 'city';
            } else if ($parent['level'] == 'city') {
                $params['level'] = 'district';
            }
        }
        $result = Db::transaction(function () use ($id, $params, $pk) {
            $count = 0;
            foreach ($this->model->whereIn($pk, $id)->cursor() as $row) {
                $params[$pk] = $row->$pk;
                $this->svalidate($params);
                $count += $row->save($params);
            }
            return $count;
        });

        if ($result) {
            return success('更新成功', $result);
        } else {
            return error('更新失败');
        }
    }
}
